/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.shanika;

import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.FrameGrabber.Exception;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import objectExtraction.HSVSkinDetector;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author HOME
 */
public class NewSkin {

    HSVSkinDetector hs = new HSVSkinDetector();
    // 

    public static void main(String[] args) {
        try {
            CanvasFrame can = new CanvasFrame("Org Image");
            CanvasFrame can2 = new CanvasFrame("Background");
CanvasFrame can3 = new CanvasFrame("foreground");
            NewSkin ns = new NewSkin();
             HSVSkinDetector hsv = new HSVSkinDetector();
            FrameGrabber grabber = new VideoInputFrameGrabber(0);
            IplImage result;


            IplImage[] imarr = new IplImage[30];

            grabber.start();

            Thread.sleep(1000);

            for (int i = 0; i < imarr.length; i++) {
                IplImage im1 = grabber.grab();
                imarr[i] = im1;
                can.showImage(im1);
                System.out.println("upper" + i);

            }
            result = ns.average(imarr);
            can2.showImage(result);
            
            while(true){
               IplImage im1 = grabber.grab();
               IplImage diff = cvCreateImage(im1.cvSize(), IPL_DEPTH_32F, 1);
                IplImage skin = cvCreateImage(im1.cvSize(), IPL_DEPTH_32F, 1);
                   cvConvertScale(hsv.getHSVSkin(im1), skin, 1, 0);
               cvAbsDiff(skin, result, diff);
               can3.showImage(diff);
            }

        } catch (InterruptedException ex) {
            Logger.getLogger(NewSkin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(NewSkin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public IplImage average(IplImage[] imga) {

        IplImage im = imga[0];
        CvSize sz = im.cvSize();

        IplImage sum = cvCreateImage(sz, IPL_DEPTH_32F, 1);
        IplImage mean = cvCreateImage(sz, IPL_DEPTH_32F, 1);
        IplImage img = cvCreateImage(sz, IPL_DEPTH_32F, 1);

        for (int i = 0; i < imga.length; i++) {

            img = hs.getHSVSkin(imga[i]);
            cvAcc(img, sum, null);
            System.out.println(i);
        }

        cvConvertScale(sum, mean, 0.0001, 1);
    cvReleaseImage(sum);
    cvReleaseImage(img);
        return mean;

    }
}
